Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Xml

Public NotInheritable Class RequestBuilder

    Private Shared ReadOnly _SETTINGS As New XmlWriterSettings()

    Private Shared ReadOnly RE_SERVICE_TOOL As New Regex("^\d\d$")
    Private Shared ReadOnly RE_LANGUAGE_CODE As New Regex("^[a-zA-Z]{2}$")

    Public Shared ReadOnly Property Settings() As XmlWriterSettings
        Get
            Return _SETTINGS
        End Get
    End Property

    Public Shared Function BuildInformationRequest(ByVal tool As String, ByVal modality As String, ByVal model As String, ByVal language As String, ByVal sessionId As String, ByVal keys() As String, ByVal docElemName As String, ByVal pubId As String, ByVal sysId As String, ByVal subset As String) As String

        'Dim sb As New StringBuilder()
        'Dim xw As XmlWriter = XmlWriter.Create(sb, SETTINGS)
        Dim ms As New MemoryStream()
        Dim xw As XmlWriter = XmlWriter.Create(ms, _SETTINGS)

        xw.WriteStartDocument()

        If docElemName IsNot Nothing Then
            xw.WriteDocType(docElemName, pubId, sysId, subset)
        End If

        xw.WriteStartElement("request")
        xw.WriteAttributeString("type", "information")

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "tool")
        xw.WriteString(tool)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "modality")
        xw.WriteString(modality)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "model")
        xw.WriteString(model)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "internationalization")
        xw.WriteString(language)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "session-id")
        xw.WriteString(sessionId)
        xw.WriteEndElement()

        Dim index As Integer = 0
        For Each key As String In keys
            index += 1
            xw.WriteStartElement("item")
            xw.WriteAttributeString("name", "key " & index)
            xw.WriteString(key)
            xw.WriteEndElement()
        Next

        xw.WriteEndElement()

        xw.WriteEndDocument()

        xw.Close()

        'Return sb.ToString()
        ms.Position = 0
        Using sr As New StreamReader(ms)
            Return sr.ReadToEnd()
        End Using

    End Function

    Public Shared Function BuildFileRequest(ByVal tool As String, ByVal modality As String, ByVal model As String, ByVal identifier As String, ByVal sessionId As String, ByVal docElemName As String, ByVal pubId As String, ByVal sysId As String, ByVal subset As String) As String

        'Dim sb As New StringBuilder()
        'Dim xw As XmlWriter = XmlWriter.Create(sb, SETTINGS)
        Dim ms As New MemoryStream()
        Dim xw As XmlWriter = XmlWriter.Create(ms, _SETTINGS)

        xw.WriteStartDocument()

        If docElemName IsNot Nothing Then
            xw.WriteDocType(docElemName, pubId, sysId, subset)
        End If

        xw.WriteStartElement("request")
        xw.WriteAttributeString("type", "file")

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "tool")
        xw.WriteString(tool)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "modality")
        xw.WriteString(modality)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "model")
        xw.WriteString(model)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "identifier")
        xw.WriteString(identifier)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "session-id")
        xw.WriteString(sessionId)
        xw.WriteEndElement()

        xw.WriteEndElement()

        xw.WriteEndDocument()

        xw.Close()

        'Return sb.ToString()
        ms.Position = 0
        Using sr As New StreamReader(ms)
            Return sr.ReadToEnd()
        End Using

    End Function

    Public Shared Function BuildSessionRequest(ByVal tool As String, ByVal modality As String, ByVal model As String, ByVal docElemName As String, ByVal pubId As String, ByVal sysId As String, ByVal subset As String) As String

        'Dim sb As New StringBuilder()
        'Dim xw As XmlWriter = XmlWriter.Create(sb, SETTINGS)
        Dim ms As New MemoryStream()
        Dim xw As XmlWriter = XmlWriter.Create(ms, _SETTINGS)

        xw.WriteStartDocument()

        If docElemName IsNot Nothing Then
            xw.WriteDocType(docElemName, pubId, sysId, subset)
        End If

        xw.WriteStartElement("request")
        xw.WriteAttributeString("type", "session")

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "tool")
        xw.WriteString(tool)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "modality")
        xw.WriteString(modality)
        xw.WriteEndElement()

        xw.WriteStartElement("item")
        xw.WriteAttributeString("name", "model")
        xw.WriteString(model)
        xw.WriteEndElement()

        xw.WriteEndElement()

        xw.WriteEndDocument()

        xw.Close()

        'Return sb.ToString()
        ms.Position = 0

        Using sr As New StreamReader(ms)
            Return sr.ReadToEnd()
        End Using

    End Function

    Public Shared Function ValidateServiceTool(ByVal value As String) As String

        Dim msg As String

        If RE_SERVICE_TOOL.IsMatch(value) Then
            msg = Nothing
        Else
            msg = "two-digit integer expected"
        End If

        Return msg

    End Function

    Public Shared Function ValidateModality(ByVal value As String) As String

        Dim msg As String

        If value IsNot Nothing AndAlso value.Length > 0 Then
            msg = Nothing
        Else
            msg = "modality code expected"
        End If

        Return msg

    End Function

    Public Shared Function ValidateModel(ByVal value As String) As String

        Dim msg As String

        If value IsNot Nothing AndAlso value.Length > 0 Then
            msg = Nothing
        Else
            msg = "system model expected"
        End If

        Return msg

    End Function

    Public Shared Function ValidateLanguage(ByVal value As String) As String

        Dim msg As String

        If RE_LANGUAGE_CODE.IsMatch(value) Then
            msg = Nothing
        Else
            msg = "two-letter language code expected"
        End If

        Return msg

    End Function

    Public Shared Function ValidateSessionId(ByVal value As String) As String

        Dim msg As String

        If value IsNot Nothing AndAlso value.Length > 0 Then
            msg = Nothing
        Else
            msg = "session id expected"
        End If

        Return msg

    End Function

    Public Shared Function ValidateIdentifier(ByVal value As String) As String

        Dim msg As String

        If value IsNot Nothing AndAlso value.Length > 0 Then
            msg = Nothing
        Else
            msg = "identifier expected"
        End If

        Return msg

    End Function

    Private Sub New()

    End Sub
End Class
